﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Xml;
using System.Data.SqlClient;
using System.Text;
using System.Security;
using System.Web.Security;

using Webchatting.Dal;
using System.Configuration;
using System.Data.OleDb;
using System.Web;
using Webchatting.Common;
using System.Collections;
namespace Webchatting.Dal
{
    public class OperationBU
    {
        #region 变量
        public static readonly string connStr = (ConfigurationManager.ConnectionStrings["connStr"].ConnectionString);

        public SqlConnection ConnSql
        {
            set;
            get;
        }
        #endregion

        public OperationBU()
        {
            this.ConnSql = new SqlConnection(connStr);
        }
        //设置昵称
        public int AddBUser(string BUId, string BUNickName)
        {
            //TODO
            int Id = int.Parse(BUId);
            SqlParameter[] sqlParams = new SqlParameter[2];
            sqlParams[0] = new SqlParameter("@BUId", Id);
            sqlParams[1] = new SqlParameter("@BUNickName", BUNickName);

            SqlHelper.ExecuteNonQuery(ConnSql, CommandType.StoredProcedure, "spAddBUser", sqlParams);
            return 1;
        }
        
        //加载群成员
        public string LoadChattingRoomMem(int a) 
        {
            StringBuilder sb = new StringBuilder();
            SqlDataReader dr = SqlHelper.ExecuteReader(ConnSql, CommandType.Text,
               "SELECT * FROM BUser");

            sb.Append("<ul >大众聊天室成员");
            while (dr.Read())
            {
                Encoding edUTF = Encoding.UTF8;
                Encoding edGB = Encoding.GetEncoding("GB2312");
               
                sb.Append("<li id='li" + dr["BUNickName"].ToString().Trim() + "(" + dr["BUId"].ToString().Trim() + ")" +
                    "' onmouseover='this.style.backgroundColor=\"#F3FBFE\"' onmouseout='this.style.backgroundColor=\"#d6dff7\"'>"+ dr["BUNickName"].ToString().Trim() + "(" + dr["BUId"].ToString().Trim() + ")" +"</li>");

            }
            sb.Append("</ul>");
            dr.Close();
            ConnSql.Close();
            return sb.ToString();
        }

        //退出聊天室
        public int  logout(string BUId)
        {
           
           
            return SqlHelper.ExecuteNonQuery(ConnSql, CommandType.Text, "Delete From ChattingDB.dbo.BUser Where BUId ='"+BUId+"'");
        }

      
        public int SendChattingRoomMessagesMethod(int BUId,string msgContent,string BUNickName)
        {

            SqlParameter[] sqlParams = new SqlParameter[3];
            sqlParams[0] = new SqlParameter("@BUId", BUId);
            sqlParams[1] = new SqlParameter("@BMContent", msgContent);
            sqlParams[2] = new SqlParameter("@BUNickName", BUNickName);
            

          

            return SqlHelper.ExecuteNonQuery(ConnSql, "spSendChattingRoomMessages", sqlParams);
        }

        public string RecieveChattingRoomMessagesMethod(DateTime RecieveTime) 
        {
            string receiveBMessages = string.Empty;
            DateTime time2 =DateTime.Now;
            SqlDataReader dr = SqlHelper.ExecuteReader(ConnSql, CommandType.Text,
                "SELECT * FROM BMessage WHERE BMSendTime > '"+ RecieveTime+ "'AND BMSendTime< '"+time2+"'");
            while (dr.Read())
            {
                receiveBMessages += "<li>" + dr["BUNickName"].ToString().Trim() + "(" + dr["BUId"].ToString().Trim() + ")" + "   在   " + dr["BMSendTime"].ToString().Trim() + "  说<br/>" + dr["BMContent"].ToString().Trim() + "</li>";
            }

            dr.Close();

            ConnSql.Close();

            RecieveTime = time2;
            return receiveBMessages;
        }
    }

  

}
